<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div role="main" class="document">

    <div class="section" id="program-build">
        <span id="id1"></span><h1><a class="toc-backref" href="#id11">1. 程序构建</a><a class="headerlink" href="#program-build" title="永久链接至标题">¶</a></h1>
        <div class="contents topic" id="id2">
            <p class="topic-title first">目录</p>
            <ul class="simple">
                <li><a class="reference internal" href="#program-build" id="id11">程序构建</a><ul>
                    <li><a class="reference internal" href="#id3" id="id12">配置</a></li>
                    <li><a class="reference internal" href="#id4" id="id13">编译</a><ul>
                        <li><a class="reference internal" href="#makefile" id="id14">makefile编写的要点</a></li>
                        <li><a class="reference internal" href="#id5" id="id15">makefile中的全局自变量</a></li>
                        <li><a class="reference internal" href="#cmake" id="id16">更多选择 CMake</a></li>
                        <li><a class="reference internal" href="#id7" id="id17">编译依赖的库</a></li>
                        <li><a class="reference internal" href="#g" id="id18">g++编译</a></li>
                    </ul>
                    </li>
                    <li><a class="reference internal" href="#id8" id="id19">安装</a></li>
                    <li><a class="reference internal" href="#id9" id="id20">总结</a></li>
                </ul>
                </li>
            </ul>
        </div>
        <p>一般源代码提供的程序安装需要通过配置、编译、安装三个步骤；</p>
        <ol class="arabic simple">
            <li>配置做的工作主要是检查当前环境是否满足要安装软件的依赖关系，以及设置程序安装所需要的初始化信息，比如安装路径，需要安装哪些组件；配置完成，会生成makefile文件供第二步make使用；</li>
            <li>编译是对源文件进行编译链接生成可执行程序；</li>
            <li>安装做的工作就简单多了，就是将生成的可执行文件拷贝到配置时设置的初始路径下；</li>
        </ol>
        <div class="section" id="id3">
            <h2><a class="toc-backref" href="#id12">1.1. 配置</a><a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h2>
            <p>查询可用的配置选项:</p>
            <div class="highlight-default"><div class="highlight"><pre><span></span><span class="o">./</span><span class="n">configure</span> <span class="o">--</span><span class="n">help</span>
</pre></div>
            </div>
            <p>配置路径:</p>
            <div class="highlight-default"><div class="highlight"><pre><span></span><span class="o">./</span><span class="n">configure</span> <span class="o">--</span><span class="n">prefix</span><span class="o">=/</span><span class="n">usr</span><span class="o">/</span><span class="n">local</span><span class="o">/</span><span class="n">snmp</span>
</pre></div>
            </div>
            <p>–prefix是配置使用的最常用选项，设置程序安装的路径；</p>
        </div>
        <div class="section" id="id4">
            <h2><a class="toc-backref" href="#id13">1.2. 编译</a><a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h2>
            <p>编译使用make编译:</p>
            <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">make</span> <span class="o">-</span><span class="n">f</span> <span class="n">myMakefile</span>
</pre></div>
            </div>
            <p>通过-f选项显示指定需要编译的makefile；如果待使用makefile文件在当前路径，且文件名为以下几个，则不用显示指定：</p>
            <p>makefile Makefile</p>
            <div class="section" id="makefile">
                <h3><a class="toc-backref" href="#id14">makefile编写的要点</a><a class="headerlink" href="#makefile" title="永久链接至标题">¶</a></h3>
                <ul class="simple">
                    <li>必须满足第一条规则，满足后停止</li>
                    <li>除第一条规则，其他无顺序</li>
                </ul>
            </div>
            <div class="section" id="id5">
                <h3><a class="toc-backref" href="#id15">makefile中的全局自变量</a><a class="headerlink" href="#id5" title="永久链接至标题">¶</a></h3>
                <ul class="simple">
                    <li>$@目标文件名</li>
                    <li>@^所有前提名，除副本</li>
                    <li>@＋所有前提名，含副本</li>
                    <li>@＜一个前提名</li>
                    <li>@？所有新于目标文件的前提名</li>
                    <li>@*目标文件的基名称</li>
                </ul>
                <div class="admonition note">
                    <p class="first admonition-title">注解</p>
                    <p class="last">系统学习makefile的书写规则，请参考 跟我一起学makefile <a class="footnote-reference" href="#id10" id="id6">[1]</a></p>
                </div>
            </div>
            <div class="section" id="cmake">
                <h3><a class="toc-backref" href="#id16">更多选择 CMake</a><a class="headerlink" href="#cmake" title="永久链接至标题">¶</a></h3>
                <p>CMake是一个跨平台的安装（编译）工具，可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件。使用CMake，能够使程序员从复杂的编译连接过程中解脱出来。它使用一个名为 CMakeLists.txt 的文件来描述构建过程,可以生成标准的构建文件,如 Unix/Linux 的 Makefile 或Windows Visual C++ 的 projects/workspaces 。</p>
            </div>
            <div class="section" id="id7">
                <h3><a class="toc-backref" href="#id17">编译依赖的库</a><a class="headerlink" href="#id7" title="永久链接至标题">¶</a></h3>
                <p>makefile编译过程中所依赖的非标准库和头文件路径需要显示指明:</p>
                <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">CPPFLAGS</span> <span class="o">-</span><span class="n">I标记非标准头文件存放路径</span>
<span class="n">LDFLAGS</span>  <span class="o">-</span><span class="n">L标记非标准库存放路径</span>
</pre></div>
                </div>
                <p>如果CPPFLAGS和LDFLAGS已在用户环境变量中设置并且导出（使用export关键字），就不用再显示指定；</p>
                <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">make</span> <span class="o">-</span><span class="n">f</span> <span class="n">myMakefile</span> <span class="n">LDFLAGS</span><span class="o">=</span><span class="s1">'-L/var/xxx/lib -L/opt/mysql/lib'</span>
    <span class="n">CPPFLAGS</span><span class="o">=</span><span class="s1">'-I/usr/local/libcom/include -I/usr/local/libpng/include'</span>
</pre></div>
                </div>
                <div class="admonition caution">
                    <p class="first admonition-title">警告</p>
                    <p class="last">链接多库时，多个库之间如果有依赖，需要注意书写的顺序，右边是左边的前提；</p>
                </div>
            </div>
            <div class="section" id="g">
                <h3><a class="toc-backref" href="#id18">g++编译</a><a class="headerlink" href="#g" title="永久链接至标题">¶</a></h3>
                <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">g</span><span class="o">++</span> <span class="o">-</span><span class="n">o</span> <span class="n">unixApp</span> <span class="n">unixApp</span><span class="o">.</span><span class="n">o</span> <span class="n">a</span><span class="o">.</span><span class="n">o</span> <span class="n">b</span><span class="o">.</span><span class="n">o</span>
</pre></div>
                </div>
                <p>选项说明：</p>
                <ul class="simple">
                    <li>-o:指明生成的目标文件</li>
                    <li>-g：添加调试信息</li>
                    <li>-E: 查看中间文件</li>
                </ul>
                <p>应用：查询宏展开的中间文件：</p>
                <p>在g++的编译选项中，添加 -E选项，然后去掉-o选项 ，重定向到一个文件中即可:</p>
                <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">g</span><span class="o">++</span> <span class="o">-</span><span class="n">g</span> <span class="o">-</span><span class="n">E</span> <span class="n">unixApp</span><span class="o">.</span><span class="n">cpp</span>  <span class="o">-</span><span class="n">I</span><span class="o">/</span><span class="n">opt</span><span class="o">/</span><span class="n">app</span><span class="o">/</span><span class="n">source</span> <span class="o">&gt;</span> <span class="n">midfile</span>
</pre></div>
                </div>
                <p>查询应用程序需要链接的库:</p>
                <div class="highlight-default"><div class="highlight"><pre><span></span>$ldd myprogrammer
    libstdc++.so.6 =&gt; /usr/lib64/libstdc++.so.6 (0x00000039a7e00000)
    libm.so.6 =&gt; /lib64/libm.so.6 (0x0000003996400000)
    libgcc_s.so.1 =&gt; /lib64/libgcc_s.so.1 (0x00000039a5600000)
    libc.so.6 =&gt; /lib64/libc.so.6 (0x0000003995800000)
    /lib64/ld-linux-x86-64.so.2 (0x0000003995400000)
</pre></div>
                </div>
                <div class="admonition note">
                    <p class="first admonition-title">注解</p>
                    <p class="last">关于ldd的使用细节，参见 <a class="reference internal" href="../tool/ldd.html#ldd"><span class="std std-ref">ldd 查看程序依赖库</span></a></p>
                </div>
            </div>
        </div>
        <div class="section" id="id8">
            <h2><a class="toc-backref" href="#id19">1.3. 安装</a><a class="headerlink" href="#id8" title="永久链接至标题">¶</a></h2>
            <p>安装做的工作就简单多了，就是将生成的可执行文件拷贝到配置时设置的初始路径下:</p>
            <div class="highlight-default"><div class="highlight"><pre><span></span>$make install
</pre></div>
            </div>
            <p>其实 <strong>install</strong> 就是makefile中的一个规则，打开makefile文件后可以查看程序安装的所做的工作；</p>
        </div>
        <div class="section" id="id9">
            <h2><a class="toc-backref" href="#id20">1.4. 总结</a><a class="headerlink" href="#id9" title="永久链接至标题">¶</a></h2>
            <p>configure make install g++</p>
            <div class="wy-table-responsive"><table class="docutils footnote" frame="void" id="id10" rules="none">
                <colgroup><col class="label"><col></colgroup>
                <tbody valign="top">
                <tr><td class="label"><a class="fn-backref" href="#id6">[1]</a></td><td>陈皓 跟我一起写Makefile <a class="reference external" href="http://scc.qibebt.cas.cn/docs/linux/base/%B8%FA%CE%D2%D2%BB%C6%F0%D0%B4Makefile-%B3%C2%F0%A9.pdf">http://scc.qibebt.cas.cn/docs/linux/base/%B8%FA%CE%D2%D2%BB%C6%F0%D0%B4Makefile-%B3%C2%F0%A9.pdf</a></td></tr>
                </tbody>
            </table></div>
        </div>
    </div>


</div>
</body>
</html>